<?php
/*
+ ----------------------------------------------------------------------------------------------+
|     e107 website system  : http://e107.org.ru
|     Released under the terms and conditions of the GNU General Public License (http://gnu.org).
|
|     Plugin "roll_mini"
|     Author: Alex ANP alex-anp@ya.ru
|     Home page: http://e107plugins.blogspot.com
+-----------------------------------------------------------------------------------------------+
*/

require_once("../../class2.php");

$lan_file = e_PLUGIN."roll_mini/languages/".e_LANGUAGE.".php";
include_once((file_exists($lan_file) ? $lan_file : e_PLUGIN."roll_mini/languages/English.php"));

require_once(e_HANDLER."ren_help.php");
require_once(HEADERF);

$index_size = $pref['roll_m_index_size'];
$cards_in_page = $pref['roll_m_cards_in_page'];
$show_full_index = $pref['roll_m_fullindex_show'];

if ($_GET['cat']) $id_cat = $_GET['cat']; else $id_cat = "1";
if ($_GET['add']) $add = $_GET['add'];
if ($_GET['edit']) $edit = $_GET['edit'];
if ($_GET['card_id']) $card_id = $_GET['card_id'];
if ($_GET['index_name']) $index_name = $_GET['index_name'];
if ($_GET['search']) $search = $_GET['search'];
if ($_GET['page']) { $page = $_GET['page']; } else { $page = 1; }


$mydb = new db();

if ($pref['roll_m_comment_show']) {
require_once(e_HANDLER."comment_class.php");
$cobj = new comment;
}

// ============== Add Card Script =============
if(getperms("P") && IsSet($_POST['insert_card'])) {

    $index_id = $_POST['index_id'];
	$name = $_POST['name'];
    	$name = $tp->toDB($name);
    if (CHARSET == "utf-8" && (strlen($name) > strlen(utf8_decode($name)))) $index_size = $index_size * 2;
	$index_name = substr($name, 0, $index_size);
    	$index_name = strtoupper($index_name);
        	$index_name = $tp->toDB($index_name);
	$description = $_POST['description'];
    	$description = $tp->toDB($description);
	$description_2 = $_POST['description_2'];
    	$description_2 = $tp->toDB($description_2);
    $id_cat = $_POST['id_cat'];
    	$id_cat = $tp->toDB($id_cat);
	$message = "Card - ".$name.", ".$description.", add.";
	$table = "roll_mini_cards";
	$values = array(
        "index_name" => $index_name,
        "name" => $name,
        "description" => $description,
        "description_2" => $description_2,
        "id_cat" => $id_cat
        );
	$mydb->db_Insert($table, $values);
	}

// ============== Edit Card Script =============
if(getperms("P") && IsSet($_POST['update_card'])) {

    $card_id = $_POST['card_id'];
	$name = $_POST['name'];
    if (CHARSET == "utf-8" && (strlen($name) > strlen(utf8_decode($name)))) $index_size = $index_size * 2;
    $index_name = substr($name, 0, $index_size);
    	$index_name = strtoupper($index_name);
	$description = $_POST['description'];
	$description_2 = $_POST['description_2'];
    $id_cat = $_POST['id_cat'];

    $name = $tp->toDB($name);
    $index_name = $tp->toDB($index_name);
    $description = $tp->toDB($description);
	$description_2 = $tp->toDB($description_2);
    $id_cat = $tp->toDB($id_cat);

	$message = "Card ID - ".$card_id." Index Name - ".$index_name." Card Name - ".$name.", Updated.";
    $mydb->db_Update("roll_mini_cards", "index_name='$index_name', name='$name', description='$description', description_2='$description_2', id_cat='$id_cat' WHERE id='$card_id'");
	$index_name = "";
    }

// ============== Delete Card Script =============
if(getperms("P") && IsSet($_POST['delete_card'])) {

    $card_id = $_POST['card_id'];
	$name = $_POST['name'];
	$description = $_POST['description'];
	$message = "Card - ".$card_id.", ".$name.", ".$description.", Deleted.";
	$mydb->db_Delete("roll_mini_cards", "id='$card_id'");
	$card_id = "";
	}
// ============= Comment Add script ================
if (isset($_POST['commentsubmit']) && $pref['roll_m_comment_show']) {

//           enter_comment($author_name, $comment, $table, $id, $pid, $subject, $rateindex=FALSE)
	$cobj -> enter_comment($_POST['author_name'], $_POST['comment'], 'roll_mini', $card_id, 0, $_POST['subject'], $rateindex=FALSE);

}

// ============= Add Card Form ====================
if (getperms("P") && $add == "1") {
    $card_text .= "<a name='add'></a>
	<form name='add_card' method='post' action='".e_SELF."?cat=".$id_cat."'><table>\n";

if ($pref['roll_m_categoryes']) {

    $card_text .= "	<tr>
        <td>".ROLL_M_L032.":</td>
  		<td>
            <select class='tbox' name='id_cat'>
            ";

$mydb->db_Select("roll_mini_cat", "*", "id > 0 order by id");

while($row = $mydb->db_Fetch()) {

    $card_text .= "<option value='".$row['id']."' ".($row['id'] == $id_cat ? "selected='selected'" : "").">".$row['name']."</option>";

}
    $card_text .= "
            </select>
        </td>
	</tr>";
}

    $card_text .= "
    <tr>
        <td>".ROLL_M_L005."</td>
		<td><input type='text' name='name' value='".$name."' class='tbox' style='width: 200px;'></td>
	</tr>
	<tr>
    	<td>".ROLL_M_L006."</td>
        <td>
			<textarea class='tbox' id='description' name='description' rows='8' onselect='storeCaret(this);' onclick='storeCaret(this);' onkeyup='storeCaret(this);' style='width: 500px;'>".$description."</textarea>
		</td>
	</tr>
	<tr>
    	<td>".ROLL_M_L020."</td>
        <td>
			<textarea class='tbox' id='description' name='description_2' rows='12' onselect='storeCaret(this);' onclick='storeCaret(this);' onkeyup='storeCaret(this);' style='width: 500px;'>".$description_2."</textarea>
		</td>
	</tr>
    <tr>
        <td>  </td>
        <td>".display_help()." </td>
    </tr>
    <tr>
        <td> </td>
    	<td>
            <input type='submit' name='insert_card' value='".ROLL_M_L013."' class='button'>
		</td>
	</tr>
	</table></form>\n
    ";
    }

// ============== Edit Card Form ===================
if (getperms("P") && $edit == "1" && $card_id != "") {

	$mydb->db_Select("roll_mini_cards", "*", "id = $card_id");
	while($row = $mydb->db_Fetch()) {
	$name = $row['name'];
	$description = $row['description'];
	$description_2 = $row['description_2'];
	$id_cat = $row['id_cat'];
    }
	$card_text .= "<a name='edit'></a>
    <form name='edit_card' method='post' action='".e_SELF."?cat=".$id_cat."' ><table>
    <tr>
    	<td>
            Card ID</td><td>".$card_id."
            <input type='hidden' name='card_id' value='$card_id'>
        </td>
	</tr>";

if ($pref['roll_m_categoryes']) {

    $card_text .= "	<tr>
        <td>".ROLL_M_L032.":</td>
  		<td>
            <select class='tbox' name='id_cat'>";

$mydb->db_Select("roll_mini_cat", "*", "id > 0 order by id");

while($row = $mydb->db_Fetch()) {

    $card_text .= "<option value='".$row['id']."' ".($row['id'] == $id_cat ? "selected='selected'" : "").">".$row['name']."</option>";

}
    $card_text .= "
            </select>
        </td>
	</tr>";
}

	$card_text .= "
	<tr>
        <td> ".ROLL_M_L005."</td>
		<td><input type='text' name='name' value='".$name."' class='tbox' style='width: 200px;'></td>
	</tr>
	<tr>
    	<td>".ROLL_M_L006."</td>
        <td>
			<textarea class='tbox' id='description' name='description' rows='8' onselect='storeCaret(this);' onclick='storeCaret(this);' onkeyup='storeCaret(this);' style='width: 500px;'>".$description."</textarea>
		</td>
	</tr>
	<tr>
    	<td>".ROLL_M_L020."</td>
        <td>
			<textarea class='tbox' id='description' name='description_2' rows='12' onselect='storeCaret(this);' onclick='storeCaret(this);' onkeyup='storeCaret(this);' style='width: 500px;'>".$description_2."</textarea>
		</td>
	</tr>
    <tr>
        <td>  </td>
        <td>".display_help()."</td>
    </tr>
    <tr>
        <td>  </td>
    	<td>
			<input type='submit' name='update_card' value='".ROLL_M_L013."' class='button'>
			<input type='submit' name='delete_card' value='".ROLL_M_L012."' class='button'>
		</td>
	</tr>
	</table></form>"; 
			
	$card_text .= "";	
	} 

// ================= Search Form =================
$search_text = "
<div id='roll_search'>
<form name='roll_search' action='".e_SELF."' method='get'>
<input class='tbox' type='hidden' name='cat' size='30' value='".$id_cat."' >
<input class='tbox' type='text' name='search' size='30' value='".$search."' >
<input class='button' type='submit' name='roll_search' value='".ROLL_M_L026."'>
</form>
</div>
";

// ================= Index select =======================
$index_text .= "<div id='index_list'><a href='".e_SELF."?cat=".$id_cat."'>".ROLL_M_L014."</a> \n";
if ($show_full_index) { $where = "name = '-roll_mini_system_name-' or id_cat = '$id_cat'"; } else { $where = "name != '-roll_mini_system_name-' and id_cat = '$id_cat'"; }
$mydb->db_Select("roll_mini_cards", "index_name", "$where GROUP BY index_name ORDER BY index_name");
$a_index_name = array();
while($row = $mydb->db_Fetch()) {
   $a_index_name[] = $row['index_name'];
   }

//sort($a_index_name);

$mydb->db_Select("roll_mini_cards", "index_name, count(*) as index_count", "id_cat = '$id_cat' AND name != '-roll_mini_system_name-' GROUP BY index_name");

$count = array();
while($row = $mydb->db_Fetch()) {
    $count[$row['index_name']] = $row['index_count'];

}
foreach ($a_index_name as $index) {
    if ($count[$index] > 0) {
        $index_text .= "<a href='".e_SELF."?cat=".$id_cat."&index_name=".$index."'>".$index."</a> \n";
        } else {
            $index_text .= "".$index." \n";
            }
}

$index_text .= "</div>";

// ================= Cards select =======================
$where = "id > 0 AND name != '-roll_mini_system_name-' and id_cat = '$id_cat' ORDER BY name";
if ($card_id != "") $where = "id = '$card_id' and id_cat = '$id_cat' ORDER BY name";
if ($index_name != "") $where = "index_name = '$index_name' and id_cat = '$id_cat'  AND name != '-roll_mini_system_name-' ORDER BY name";
if ($search != "") $where = "(name LIKE '%$search%' OR  description LIKE '%$search%' OR description_2 LIKE '%$search%') AND name != '-roll_mini_system_name-' and id_cat = '$id_cat'";

$mydb->db_Select("roll_mini_cards", "*", $where);

$card_text .= " \n <!-- #### Card text START ### --> \n ";
$a_id = array();
$a_name = array();
$a_description = array();
$a_description_2 = array();
$cards_selected = 0;
		
		while($row = $mydb->db_Fetch()) {
		   $a_id[] = $row['id'];
           $a_name[] = $row['name'];
           if ($card_id != "" and $pref['roll_m_comment_show']) $comment_subj = $row['name'];
           $a_description[] = $row['description'];
           $a_description_2[] = $row['description_2'];
           $cards_selected++;
           }

$i_start = ($page-1) * $cards_in_page;
$i_end = $page*$cards_in_page; 
if ($i_end > $cards_selected) $i_end = $cards_selected;   
for ($i=$i_start; $i<$i_end; $i++) {
           // Name
		   $card_text .= "<div id='card_list'> \n <div id='card_list_name'>\n ".$a_name[$i]."";
           if (getperms("P")) $card_text .= " <a href='".e_SELF."?cat=".$id_cat."&card_id=".$a_id[$i]."&edit=1#edit'>[".ROLL_M_L011."]</a>";
           $card_text .= "\n</div>\n";
		   // Description
		   $text = $a_description[$i];
		   $text = $tp->toHTML($text, true);
           $card_text .= "<div id='card_list_description'>\n".$text."\n ";
            if ($card_id == "" && $a_description_2[$i] != "") {
                $card_text .= " <a href='".e_SELF."?cat=".$id_cat."&card_id=".$a_id[$i]."'>".ROLL_M_L021."</a>";
                }   
           $card_text .= "</div>\n";
        if ($card_id != "") {
           $text_2 = $a_description_2[$i];
		   $text_2 = $tp->toHTML($text_2, true);
           $card_text .= "<div id='card_list_description'>\n".$text_2."\n</div>\n ";

        }
        $card_text .= "</div> \n ";
}

// ========== Page navigation ===================
if (($cards_selected/$cards_in_page)>1) {
    $card_text .= "<div id='page_list_nav'>\n";
    if ($page > 1) {
        $backe = $page - 1;
        $card_text .= "<a href='".e_SELF."?cat=".$id_cat."&page=".$backe."";
        if ($index_name != "") $card_text .= "&index_name=".$index_name."";
        $card_text .= "'>&lt;&lt;&lt;</a> ";
        }
    for ($i=1; $i<(1+$cards_selected/$cards_in_page); $i++){
        if ($i == $page) {
            $card_text .= " [".$i."] ";
            } else {
            $card_text .= " <a href='".e_SELF."?cat=".$id_cat."&page=".$i."";
            if ($index_name != "") $card_text .= "&index_name=".$index_name."";
            $card_text .= "'>[".$i."]</a> ";
            }
        }
    $card_text .= "".$i_start + 1 ."-".$i_end."/".$cards_selected."";
    if ($page < ($i-1)) {
        $next = $page + 1;
        $card_text .= " <a href='".e_SELF."?cat=".$id_cat."&page=".$next."";
        if ($index_name != "") $card_text .= "&index_name=".$index_name."";
        $card_text .= "'>&gt;&gt;&gt;</a> ";
    }
$card_text .= "</div> \n";
}
$card_text .= "<!-- #### Card text END ### -->\n";


// =========== Render Page	==================
// Search
if ($pref['roll_m_search_show']) echo $search_text;

// Index List
$caption = ROLL_M_L003;
$text = $index_text;
$ns -> tablerender($caption, $text);

// Cards List

/*if (!getperms("P")) {
echo "<script>
var bbb=1;
function rf(){ return false }
if(document.layers || (window.captureEvents && !(document.all))){
 document.captureEvents(Event.MOUSEDOWN);
 document.onmousedown=rf;
 self.blur()
 bbb=2;
 }
else if (window.captureEvents){
 document.write(\"<input type=Text style='visibility:hidden;position:absolute' id=ht onblur='this.focus()'>\");
 ht.focus();
}
else if(document.all)
 document.onselectstart=rf;
document.oncontextmenu=rf;
if(window.opener!=undefined&&window.opener.location.host==location.host&&location.host!='') bbb=0;
</script>";
} */

$mydb->db_Select("roll_mini_cat", "*", "id = '$id_cat' order by id");
while($row = $mydb->db_Fetch()) {
    $caption = $row['name'];
}

//$caption = $pref['roll_m_name'];
if (getperms("P")) $caption .= " <a href='".e_SELF."?cat=".$id_cat."&index_id=$index_id&add=1&card=1#add'>[".ROLL_M_L010."]</a>";
$text = $card_text;
$ns -> tablerender($caption, $text);

if ($card_id != "" and $edit != "1" and $pref['roll_m_comment_show']) {
   $card_text .= $cobj -> compose_comment("roll_mini", "comment", $card_id, "", $comment_subj, $rate=FALSE, $return=FALSE, $tablerender=TRUE);
}

// Debug List
if (getperms("P")) {
    $caption = "".ROLL_M_L015."";
    $text = "Add - $add  Edit - $edit <br>Category - $id_cat Card - $card_id Index Name - $index_name ";
    $text .= "<br>Total Cards in Base - ".$mydb->db_Count("roll_mini_cards", "(*)", "WHERE name != '-roll_mini_system_name-'")."<br>";
    $str = $index_name;
    $text .= "<br>CHR - ".ord($str)."";
    $text .= "<br>Charset - ".CHARSET."";
    $text .= "<br><b>".$message."</b>";

    $ns -> tablerender($caption, $text);
    }

require_once(FOOTERF);
?>